package com.wangling.servlet;

import com.wangling.model.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/logout")
public class LogoutServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");

        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html");
        PrintWriter writer = resp.getWriter();

        HttpSession session = req.getSession(false);
        if (session != null) {
            session.removeAttribute("currentUser");
        } else {
            writer.printf("<p>请登录后操作,点击<a href='/blog0123/login.html'>此处</a>进行登录</p>");
        }

        resp.sendRedirect("/blog0124/login.html");
    }
}
